<?php

declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\TransactionCurrency as TransactionCurrencyModel;
use app\admin\common\ServiceResponse;

class TransactionCurrency extends BaseService
{

    use ServiceResponse;

    protected $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = new TransactionCurrencyModel();
    }

    /**
     * @describe 获取交易币种列表
     * @author: Bobo
     * @date: 2024-06-05 18:02:26
     * @param [] type
     * @return Renderable
     */
    public function getList(array $where, int $page, int $limit)
    {
        if (0 >= $page || 100 < $limit) {
            return $this->error(100002);
        }

        $total = $this->model::where($where)->count();
        $list = $this->model
            ->where($where)
            ->page($page, $limit)
            ->select()
            ->toArray();
        $data = ['list' => $list, 'total' => $total];
        return $this->success($data);
    }

    /**
     * 获取交易币种详情
     * @param int $id 交易币种ID
     */
    public function getOneById($id)
    {
        $TransactionCurrency = $this->model::find($id);
        if ($TransactionCurrency) {
            $data = $TransactionCurrency->append(['creator_value', 'updater_value',]);
        }
        $data = $data->toArray();
        return $this->success($data);
    }

    /**
     * 创建交易币种
     * @param array $data 创建交易币种的数据
     */
    public function create($data)
    {

        try {
            $res = $this->model::find([
                'name' => $data['name'],
                'is_del' => 0
            ]);
            if ($res) {
                return $this->error(100300);
            }
            $result = $this->model::create($data);
            if ($result) {
                $this->logger->logOperation($result->id, 'TransactionCurrency', '添加', '创建交易币种');
                return $this->success();
            } else {
                return $this->error(500001);
            }
        } catch (\Throwable $th) {
            return $this->error(500001, '服务器内部错误：' . $th->getMessage());
        }
    }

    /**
     * 更新交易币种
     * @param int $id 交易币种ID
     * @param array $data 更新交易币种的数据
     */
    public function update($data)
    {
        $res = $this->model::where('id', $data['id'])->find();
        if (!$res) {
            return false;
        }
        foreach ($data as $key => $value) {
            if ($res->$key != $value) {
                $res->$key = $value;
            }
        }

        $result = $res->save();
        if ($result) {
            $this->logger->logOperation($res->id, 'TransactionCurrency', '更新', '更新交易币种');
            return $this->success();
        } else {
            return $this->error(500001);
        }
    }

    /**
     * 删除交易币种
     * @param int $id 交易币种ID
     */
    public function delete($id)
    {
        return $this->model::where('id', $id)->delete();
    }

    /**
     * @describe 批量逻辑删除交易币种
     * @author: Bobo
     * @date: 2024-06-26 14:53:00
     * @param [arr] @required $ids ID数组
     * @return Renderable
     */
    public function batchLogicalDelete($ids)
    {
        try {
            $this->model::where('id', 'in', $ids)->update(['is_del' => time()]);
            $this->logger->logOperation($ids, 'TransactionCurrency', '删除', '批量逻辑删除交易币种');
            return $this->success();
        } catch (\Throwable $th) {
            return $this->error(500001, $th->getMessage());
        }
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2024-08-06 19:08:17
     * @param [type] @required
     * @return Renderable
     */
    /**
     * @describe
     * @author: Bobo
     * @date: 2024-07-23 10:34:00
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum($field)
    {
        $res = $this->model->getFieldEnum($field);
        return [
            'code' => 0,
            'data' => [
                'list' => $res
            ]
        ];
    }


    /**
     * @describe
     * @author: Bobo
     * @date: 2024-08-28 14:00:28
     * @param [type] @required
     * @return Renderable
     */
    public function status($params)
    {
        $res = $this->model->where('id', $params['id'])->find();
        if (!$res) {
            return false;
        }
        $res->status =  !$res->status;
        $res->updater =  $params['updater'];
        $res->update_time =  $params['update_time'];

        $result = $res->save();
        if ($result) {
            $this->logger->logOperation($res->id, 'TransactionCurrency', '更新', '更新交易币种');
            return $this->success();
        } else {
            return $this->error(500001);
        }
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2024-09-14 14:29:02
     * @param [type] @required
     * @return Renderable
     */
    public function getSelectOption(array $where, $fields = 'id, name')
    {
        $list = $this->model->getSelectOption($where, "id,CONCAT(name,' (',abbr,') ') as name");
        return $this->success(['list' => $list]);
    }
}
